import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.util.List;
import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ActionHouseClient {
	private InitialContext iiopContext;

	private static PriceList priceList;
	private JPanel urlPanel;

	private static final String IIOP_STRING = "com.sun.jndi.cosnaming.CNCtxFactory";
	private static String url;

	/**
	 * Start the client
	 * 
	 * @param args
	 *            client arguments that aren't used in any way. So don't bother!
	 */
	public static void main(String[] args) {

		ActionHouseClient ahc = new ActionHouseClient();
		System.setProperty("java.security.policy", "client.policy");
		System.setSecurityManager(new RMISecurityManager());

		try {
			ahc.createGui();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Creates the GUI for the client
	 * 
	 * @throws UnknownHostException
	 */
	public void createGui() throws UnknownHostException {
		JFrame guiFrame = new JFrame("ESA-RMI Client");
		guiFrame.setTitle("ESA-RMI Client");
		guiFrame.setVisible(true);
		guiFrame.setSize(420, 300);
		guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		guiFrame.setLayout(new BorderLayout(2, 2));

		JPanel displayPanel = new JPanel(new BorderLayout(1, 1));

		// create panel for display
		final DefaultListModel<String> model = new DefaultListModel<String>();
		final JList<String> list = new JList<String>(model);
		final JButton orderButton = new JButton("Order");
		list.setPreferredSize(new Dimension(300, 200));
		orderButton.setPreferredSize(new Dimension(100, 200));
		orderButton.setEnabled(false);
		orderButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				int[] orderIndices = list.getSelectedIndices();
				try {

					Order orderList = (Order) PortableRemoteObject.narrow(
							iiopContext.lookup("orderlist"), Order.class);

					System.out.println(orderList);
					for (int itemIndex : orderIndices) {
						Property p = priceList.getPriceList().get(itemIndex);
						orderList.orderItems(p);
						System.out.println("Ordering " + p.getMyKey() + " for "
								+ p.getMyValue());
					}

				} catch (NamingException e) {
					e.printStackTrace();
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		});
		displayPanel.add(list, BorderLayout.WEST);
		displayPanel.add(orderButton, BorderLayout.EAST);

		// Url Panel
		urlPanel = new JPanel(new BorderLayout(1, 1));
		final JButton connectButton = new JButton("Connect");
		final JTextField urlAddress = new JTextField();
		// set settings
		urlAddress.setPreferredSize(new Dimension(300, 32));
		urlAddress.setText(InetAddress.getLocalHost().getHostAddress()
				.toString());
		connectButton.setPreferredSize(new Dimension(100, 32));
		connectButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {

					url = "iiop://" + urlAddress.getText() + ":9000";

					Properties iiopProperties = new Properties();
					iiopProperties.put("java.naming.factory.initial",
							ActionHouseClient.IIOP_STRING);
					iiopProperties.put("java.naming.provider.url",
							ActionHouseClient.url);

					iiopContext = new InitialContext(iiopProperties);
					priceList = (PriceList) PortableRemoteObject.narrow(
							iiopContext.lookup("pricelist"), PriceList.class);
				} catch (NamingException e1) {
					e1.printStackTrace();
				}

				System.out.println("Connected to: " + url);
				model.clear();
				try {
					List<Property> items = priceList.getPriceList();
					String[] data = new String[items.size()];
					for (int i = 0; i < data.length; i++) {
						Property p = items.get(i);
						model.addElement(p.getMyKey() + " - " + p.getMyValue());
					}
					orderButton.setEnabled(true);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});
		// add to panel
		urlPanel.add(urlAddress, BorderLayout.WEST);
		urlPanel.add(connectButton, BorderLayout.EAST);

		// Add panels to the frame
		guiFrame.add(urlPanel, BorderLayout.NORTH);
		guiFrame.add(displayPanel, BorderLayout.AFTER_LAST_LINE);
		guiFrame.validate();

	}
}
